<?php
class Connexion {
    private $_bd;//Instance de PDO
    public function __construct($bd) {
      $this->setBd($bd);
    }
    public function addProbleme(Probleme $p)
    {
			$qry = $this->_bd->prepare('insert into probleme set nom=:nom,email=:email,titre=:titre,texte=:texte');
			$qry->bindValue(':nom',$p->nom());
			$qry->bindValue(':email',$p->email());
			$qry->bindValue(':titre',$p->titre());
			$qry->bindValue(':texte',$p->texte());
			$qry->execute();
			$p->hydrate(array(
							'id'=>  $this->_bd->lastInsertId(),
			 ));
    }
    public function count()
    {
			return $this->_bd->query('select count(*) from probleme')->fetchColumn();
    }
    public function delete(Probleme $p)
    {
			$this->_bd->exec('delete from probleme where id_probleme='.$p->id_probleme());
    }
    public function exists($info)
    {
			if(is_int($info))
			{
					return (bool) $this->_bd->query('select count(*) from probleme where id_probleme ='. $info)->fetchColumn();
			}
			$qry = $this->_bd->prepare('select count(*) from probleme where nom=:nom');
			$qry->execute(array(':nom'=>$info));
			return (bool) $qry->fetchColumn();
    }
    public function get($info)
    {
			if(is_int($info))
			{
				 $qry = $this->_bd->query('select * from probleme where id_probleme='.$info);
				 $donnees = $qry->fetch(PDO::FETCH_ASSOC);
				 return new Probleme($donnees);
			}else
			{
				$qry = $this->_bd->prepare('select * from probleme where nom=:nom');
				$qry->execute(array(':nom'=>$info));
				return new Probleme($qry->fetch(PDO::FETCH_ASSOC));
			}  
    }
    public function getList($nom)
    {
			$prob = array();
			$qry = $this->_bd->prepare('select * from probleme where nom <>:nom order by nom');
			$qry->execute(array(':nom'=>$nom));
			while ($donnees =$qry->fetch(PDO::FETCH_ASSOC))
			{
					$prob[]=new Probleme($donnees);
			}
			return $prob;
    }
		public function getAllProbleme()
    {
			$prob = array();
			$qry = $this->_bd->prepare('select * from probleme');
			$qry->execute();
			while ($donnees =$qry->fetch(PDO::FETCH_ASSOC))
			{
					$prob[]=new Probleme($donnees);
			}
			return $prob;
    }
    public function update(Probleme $p)
    {
			$qry = $this->_bd->prepare('update probleme set nom=:nom,email=:email,titre=:titre,texte=:texte where id_probleme =:id');
			$qry->bindValue(':id',$p->id_probleme());
			$qry->bindValue(':nom',$p->nom());
			$qry->bindValue(':email',$p->email());
			$qry->bindValue(':titre',$p->titre());
			$qry->bindValue(':texte',$p->texte());
			$qry->execute();
    }
    public function setBd($bd)
    {
			$this->_bd=$bd;
    }
}

?>
